﻿<Window
    x:Class="ItemTemplateSelector"
    xmlns:local="clr-namespace:DataBinding"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="ItemTemplateSelector" Height="380" Width="403">
    <Window.Resources>
        
        <!--ListBox will call this CustomerRoleTemplateSelector each time it is rendering a data object.
            CustomerRoleTemplateSelector will return one of the below DataTemplates based on the data object.-->
        <local:PersonProfessionDataTemplateSelector x:Key="personProfessionDataTemplateSelector" />

        <DataTemplate x:Key="nonAgentDataTemplate">
            <Border BorderBrush="LightGray" BorderThickness="2" Margin="3.5" Padding="3.5">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="85" />
                        <ColumnDefinition Width="*" />
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                    </Grid.RowDefinitions>
                    <TextBlock Foreground="Gray" FontSize="16" Text="Name: " />
                    <StackPanel Grid.Column="1" Orientation="Horizontal">
                        <TextBlock Text="{Binding Path=FirstName}" FontSize="16" />
                        <TextBlock Text="{Binding Path=LastName}"  FontSize="16" Margin="3.5,0,0,0" />
                    </StackPanel>
                    <TextBlock Grid.Row="1" Foreground="Gray" FontSize="16" Text="Profession: " />
                    <TextBlock Grid.Row="1" Grid.Column="1" Foreground="Gray" FontSize="16" Text="{Binding Path=Profession}" />
                </Grid>
            </Border>
        </DataTemplate>

        <DataTemplate x:Key="agentDataTemplate">
            <Border x:Name="bdr" BorderBrush="LightGray" BorderThickness="2" Margin="3.5" Padding="3.5">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="85" />
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="48" />
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                    </Grid.RowDefinitions>
                    <TextBlock Foreground="Gray" FontSize="16" Text="Name: " />
                    <StackPanel Grid.Column="1" Orientation="Horizontal">
                        <TextBlock Text="{Binding Path=FirstName}" FontSize="16" />
                        <TextBlock Text="{Binding Path=LastName}"  FontSize="16" Margin="3.5,0,0,0" />
                    </StackPanel>
                    <TextBlock Grid.Row="1" Foreground="Gray" FontSize="16" Text="Profession: " />
                    <TextBlock Grid.Row="1" Grid.Column="1" FontSize="16" Text="{Binding Path=Profession}" />
                    <Image Stretch="Uniform" Source="{Binding Path=Thumbnail}" Grid.Column="2" Grid.RowSpan="3" />
                    <TextBlock Grid.Row="2" Foreground="Gray" FontSize="16" Text="Birthday: " />
                    <TextBlock Grid.Row="2" Grid.Column="1" FontSize="16" Text="{Binding Path=Birthday, StringFormat=d}" />
                </Grid>
            </Border>
            <DataTemplate.Triggers>
                <DataTrigger Binding="{Binding Path=IsSelected, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBoxItem}}}" Value="True">
                    <Setter TargetName="bdr" Property="BorderThickness" Value="10" />
                </DataTrigger>
            </DataTemplate.Triggers>
        </DataTemplate>
        
        <!--this style forces the ListBoxItem content to consume all available horizontal space
            this overrides the default which is HorizontalContentAlignment=Left  -->
        <Style TargetType="{x:Type ListBoxItem}">
            <Setter Property="HorizontalContentAlignment" Value="Stretch" />
        </Style>
    </Window.Resources>
    <Grid>
        
        <!--ItemTemplateSelector runs code to select the DataTemplate that will be used to render
            a single data object.  This feature allows different data objects to be rendered using
            a different DataTemplate-->
        <ListBox ItemTemplateSelector="{StaticResource personProfessionDataTemplateSelector}" 
                 ItemsSource="{Binding Source={x:Static local:Data.People}}">
            <ListBox.Resources>
                <!--this is where the default colors for the ListBox are changed
                    try removing these three lines of code, then run the application to see the differences-->
                <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="WhiteSmoke" />
                <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent" />
                <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="{x:Static SystemColors.ControlTextColor}" />
            </ListBox.Resources>
        </ListBox>
    </Grid>
</Window>


